<?php

/**
 * 在PHP环境下使用PDO模块连接数据库;
 * Coding By GF;
 */

header("content-type:text/html;charset=utf8");

class database {

    public function connect() {

        $host = "127.0.0.1";

        $database = 'xxxxxx';

        $username = 'xxxxxx';

        $password = 'xxxxxx';

        try {

            $dsn = "mysql:host=".$host.";port=3306;charset=utf8;dbname=".$database;
		
            $pdo = new PDO($dsn, $username, $password);

            return $pdo;

        }catch (PDOException $e) {

            die ("Database Connection Error : ".$e->getMessage()."<br>");

        }

    }
    
    public function select() {

        $pdo = $this->connect();

        //查询Select。
	    $sql = "select * from 测试_工商银行_20101231_20201231 limit 0,10";

        try {

            $res = $pdo->query($sql);

            //返回值是一个嵌套数组，每一行为一个数组(键为行号，值为行的数组)，每一行里面又包含每个"字段=>值"的数组(键为字段，值为字段的值)。
            $data = $res->fetchAll(PDO::FETCH_ASSOC);
		    
            //var_dump($data);

            foreach ($data as $key => $value) {

                echo "第".$key."行 : ";

                //print_r($value);

                foreach ($value as $k => $v) {

                    echo $k." : ".$v." | ";

                }

                echo "<br>";

            }


        }catch (PDOException $e) {

            die ("Database Select Error : ".$e->getMessage()."<br>");

        }

    }

    public function insert(){

        $pdo = $this->connect();

        //插入Insert。
        //日期以字符串形式插入，会自动转换为datetime。
	    $sql = "insert into 测试_工商银行_20101231_20201231(日期, 名称, 代码, 开盘价) 
                values('2022-10-07', '工商银行2', '601398', 12.00)";

        try {

            if ($pdo->exec($sql)) {

                echo "Database Insert Successful <br>";

            }else {

                echo "Database Insert Failed <br>";

            }

        }catch (PDOException $e) {

            die ("Database Insert Error : ".$e->getMessage()."<br>");

        }

    }

    public function delete(){

        $pdo = $this->connect();

        //删除Delete。
	    $sql = "delete from 测试_工商银行_20101231_20201231 where 日期='2022-10-06'";

        try {

            if ($pdo->exec($sql)) {

                echo "Database Delete Successful <br>";

            }else {

                echo "Database Delete Failed <br>";

            }

        }catch (PDOException $e) {

            die ("Database Delete Error : ".$e->getMessage()."<br>");

        }

    }

    public function update(){

        $pdo = $this->connect();

        //更新Update。
	    $sql = "update 测试_工商银行_20101231_20201231 set 收盘价=20 where 日期='2022-10-07'";

        try {

            if ($pdo->exec($sql)) {

                echo "Database Update Successful <br>";

            }else {

                echo "Database Update Failed <br>";

            }

        }catch (PDOException $e) {

            die ("Database Update Error : ".$e->getMessage()."<br>");

        }

    }

}

//测试。
$classtest = new database();
//$classtest->insert();
//$classtest->delete();
//$classtest->update();
$classtest->select();

?>